/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.abnf.validation;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.xtext.validation.Check;

import de.thwildau.tm.moses.abnf.abnf.AbnfPackage;
import de.thwildau.tm.moses.abnf.abnf.BinVal;
import de.thwildau.tm.moses.abnf.abnf.HexVal;
import de.thwildau.tm.moses.abnf.abnf.Rule;
import de.thwildau.tm.moses.abnf.abnf.RuleSet;
 

public class AbnfJavaValidator extends AbstractAbnfJavaValidator {
    
    @Check
    public void checkUniqueRuleNames(Rule rule) {
        String ruleName = rule.getName();
        RuleSet ruleSet = (RuleSet) rule.eContainer();
        for(Rule otherRule : ruleSet.getRules()) {
            if(!rule.equals(otherRule)) {
                String otherRuleName = otherRule.getName();
                if(otherRuleName.equals(ruleName)) {
                    String message = "Duplicate Rule '" + ruleName + "'.";
                    error(message, rule, AbnfPackage.eINSTANCE.getRule_Name());
                    break;
                }
                if(otherRuleName.equalsIgnoreCase(ruleName)) {
                    String message = "Duplicate Rule '" + ruleName + "' and '" + otherRuleName + "', with different case.";
                    error(message, rule, AbnfPackage.eINSTANCE.getRule_Name());
                    break;
                }
            }
        }
    }
    
    @Check
    public void checkBinValues(BinVal rule) {
        checkBinValuesList(rule.getValue());
        checkBinValue(rule.getMaxValue());
    }
    
    @Check
    public void checkHexValues(HexVal rule) {
        
    }
    
    private void checkBinValuesList(EList<Long> values) {
        for (Long v : values) {
            long value = v.longValue();
            checkBinValue(value);
        }
    }
    
    private void checkBinValue(long value) {
        while(value > 0) {
            long bitValue = value % 10;
            value /= 10;
            if(!(bitValue == 0 || bitValue == 1)) {
                EStructuralFeature feature = AbnfPackage.eINSTANCE.getNumVal_Value();
                error("Binary values can only contain 0 or 1 (found " + bitValue + ").", feature);
                return;
            }
        }
    }
}
